﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;

namespace DusonFramework.ContactService.Email {
    public class EmailTomService:IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "tom.com"; }
        }

        public string StartUrl {
            get { return "http://login.mail.tom.com/cgi/login"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }
        #endregion

        #region Events

        #endregion
     

        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            string checkUserUrl = "http://bjapp3.mail.tom.com/cgi/163/check_user_status.cgi?uid="+userName+"@"+EmailSystem;
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://mail.tom.com/";
            string contentType = "application/x-www-form-urlencoded";
            string returnUrl = "";
            CookieContainer cc = new CookieContainer();
            string html = WebOpHelper.GetHtml(checkUserUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            var nvc = new NameValueCollection();
            nvc.Add("type", "0");
            nvc.Add("style", "10");
            nvc.Add("user", userName);
            nvc.Add("pass", pwd);
            nvc.Add("enter", "登　录");
            nvc.Add("verifycookie", "y");           
            string postContent = WebOpHelper.GetNameValueRowData(nvc, '&');
            html = WebOpHelper.Post(StartUrl, accept, contentType, refer,
               language, postContent, cc, Encoding.GetEncoding("gb2312"), out returnUrl);
            string sid = WebOpHelper.Resove(html, "var sid='", "'");
            string addressUrl = String.Format("http://bjapp3.mail.tom.com/cgi/ldvcapp?funcid=prtsearchres&sid={0}&listnum={1}&tempname=address/address.htm",sid,1000);
            html = WebOpHelper.GetHtml(addressUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            var address = WebOpHelper.ResoveList(html, "<a href=\"/coremail/fcg/ldmmapp?", "/a>");
            foreach (string a in address) {
                string email = WebOpHelper.Resove(a, "\">", "<");
                MailContract m = new MailContract();
                m.Email = email;
                mc.Add(m);
            }
            List<string> list2 = WebOpHelper.ResoveList(html, "<a href=\"ldvcapp?funcid=loadiadd", "/a>");
            int i = 0;
            foreach (string s in list2) {
                var s1 = WebOpHelper.Resove(s, "\">", "<");
                if (string.IsNullOrEmpty(s1)) {
                    mc[i].Name = mc[i].Email;
                }
                else {
                    mc[i].Name = s1;
                }
                i++;
            }
            return mc;

        }
    }
}
